package com.aaa.project.service.Impl;


import com.aaa.entity.OOrder;
import com.aaa.entity.UUserInfo;
import com.aaa.project.mapper.OOrderMapper;
import com.aaa.project.mapper.UUserInfoMapper;
import com.aaa.project.service.IOOrderService;
import com.aaa.util.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.Resource;
import java.security.Principal;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单信息 服务实现类
 * </p>
 *
 * @author zhangxuhui
 * @since 2023-02-03
 */
@Service
public class OOrderServiceImpl extends ServiceImpl<OOrderMapper, OOrder> implements IOOrderService {

    @Resource
    private OOrderMapper oOrderMapper;
    @Resource
    private UUserInfoMapper uUserInfoMapper;

    /**
     * 查找所有订单信息
     * @param principal
     * @param curr
     * @param pageSize
     * @param zhuangtai
     * @return
     */
    @Override
    public PageInfo findAll(Principal principal, Integer curr, Integer pageSize, Integer zhuangtai) {
        String name = principal.getName();
        // 根据当前的人的名字用户的id
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("telephone",name);
        UUserInfo uUserInfo=uUserInfoMapper.selectOne(queryWrapper);
        if(zhuangtai==null){
            zhuangtai=0;
        }
        PageHelper.startPage(curr, pageSize);
        List<OOrder> list=oOrderMapper.findAll(uUserInfo.getId(),zhuangtai);
        PageInfo pageInfo = new PageInfo(list);
        return pageInfo;
    }

    @Override
    public boolean updState(String code, Integer state) {
        return oOrderMapper.updState(code, state);
    }

    /**
     * 是否购买过商品
     * @param id
     * @param principal
     * @return
     */
    @Override
    public Object buyGoods(String id,Principal principal) {
        String name=principal.getName();
        QueryWrapper queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("telephone",name);
        UUserInfo uUserInfo=uUserInfoMapper.selectOne(queryWrapper);
        String id1 = uUserInfo.getId();
        return oOrderMapper.buyGoods(id,id1);
    }
    /**
     * 取消订单
     * state=4
     * @param id
     * @return
     */
    @Override
    public int cancle(String id) {
        return oOrderMapper.cancle(id);
    }

    /**
     * 逻辑删除订单
     * state=2
     * @param code
     * @return
     */
    @Override
    public int delete(String code) {
        return oOrderMapper.delete(code);
    }


    /**
     * 剩余时间格式化
     * @return
     */
    @PostMapping("updateTime")
    public Result updateTime(){
        return new Result<>();
    }


    @Override
    public Integer updateOOrder(String code) {
        return oOrderMapper.updateOrder(code);
    }

    @Override
    public List<OOrder> all(String code) {
        return oOrderMapper.all(code);
    }

    @Override
    public Page getAllOrderBybid(Page page, String bid, OOrder order) {
        Page ordersList = oOrderMapper.getAllOrderBybid(page,bid,order);
        return ordersList;
    }

    @Override
    public List<Map<String, Object>> income(String bid) {
        return oOrderMapper.income(bid);
    }

}
